﻿#include "servicecenter.h"
#include "databasecenter.h"

#include <QGlobalConfig>

#include <menu.h>

#include <QDir>
#include <user.h>
#include <role.h>
#include <usercontroller.h>
#include <authcontroller.h>
#include <menucontroller.h>
#include <rolecontroller.h>
#include <deptcontroller.h>
#include <dept.h>

ServiceCenter::ServiceCenter(QObject *parent) : QObject(parent)
{
    QGlobalConfig::getInstance().init(QCoreApplication::applicationDirPath() + "/" + QCoreApplication::applicationName() + ".ini");

    HttpService::getInstance();
    HttpService::getInstance().installController("auth", new AuthController(this));
    HttpService::getInstance().installController("user", new UserController(this));
    HttpService::getInstance().installController("menu", new MenuController(this));
    HttpService::getInstance().installController("role", new RoleController(this));
    HttpService::getInstance().installController("dept", new DeptController(this));

    DatabaseCenter::getInstance();
    initDataBase();
}

ServiceCenter::~ServiceCenter()
{
    //    HttpService::deleteInstance();
    qDebug() << __FUNCTION__;
    DatabaseCenter::deleteInstance();
    QGlobalConfig::deleteInstance();
}

void ServiceCenter::initDataBase()
{
    //======================================================================
    //==============================数据库初始化==========================
    //======================================================================
    QSqlDatabase db = DatabaseCenter::getInstance().getConnection();
    QSqlQuery query(db);
    //===================================================创建用户表
    User user;
    if(!query.exec(user.generateCreateTableSql(db.driverName()))){
        qCritical() << query.lastError().text();
    }
    //----插入默认数据
    user.setId(1);
    user.setUsername("yanlei");
    user.setRealName("颜磊");
    user.setPassword("123456");
    user.setGender("男");
    user.setStatus(USER_STATUS_ENABLED);//状态启用
    user.setRoles(QStringList("admin"));
    query.exec(user.generateInsertSql());

    //===================================================创建角色表
    Role role;
    if(!query.exec(role.generateCreateTableSql(db.driverName()))){
        qCritical() << query.lastError().text();
    }
    role.clear();
    //----插入默认数据
    role = Role(1,"管理员","admin",{},"管理员角色",1);
    query.exec(role.generateInsertSql());
    role = Role(2,"用户","user",{},"用户角色",1);
    query.exec(role.generateInsertSql());

    //===================================================创建部门表
    Dept dept;
    if(!query.exec(dept.generateCreateTableSql(db.driverName()))){
        qCritical() << query.lastError().text();
    }
    dept.clear();
    //----插入默认数据
    dept = Dept(1,"公司名称",0,"主体",1);
    query.exec(dept.generateInsertSql());
    dept = Dept(2,"部门一",1,"部门一",1);
    query.exec(dept.generateInsertSql());

    //===================================================创建菜单表
    Menu menu;
    if(!query.exec(menu.generateCreateTableSql(db.driverName()))){
        qCritical() << query.lastError().text();
    }

    //插入默认数据
    menu = Menu(1,"catalog","OverView","/overview","","BasicLayout",0,"",1,"概览");
    query.exec(menu.generateInsertSql());

    //概览
    menu = Menu(10,"menu","Analytics","/analytics","","/overview/analytics/index",1,"",1,"态势分析");
    query.exec(menu.generateInsertSql());
    menu = Menu(11,"menu","Workspace","/workspace","","/overview/workspace/index",1,"",2,"工作台");
    query.exec(menu.generateInsertSql());

    //系统
    menu = Menu(1000,"catalog","SystemManage","/systemmanage","","",0,"",999,"系统管理");
    query.exec(menu.generateInsertSql());
    menu = Menu(10000,"catalog","AuthManage","/authmanage","","",1000,"",0,"权限管理");
    query.exec(menu.generateInsertSql());
    menu = Menu(100001,"menu","User","/user","","/systemmanage/authmanage/user/list",10000,"",1,"用户管理");
    query.exec(menu.generateInsertSql());
    menu = Menu(100002,"menu","Role","/role","","/systemmanage/authmanage/role/list",10000,"",2,"角色管理");
    query.exec(menu.generateInsertSql());
    menu = Menu(100004,"menu","Menu","/menu","","/systemmanage/authmanage/menu/list",10000,"",4,"菜单管理");
    query.exec(menu.generateInsertSql());
    menu = Menu(100005,"menu","Dept","/dept","","/systemmanage/authmanage/dept/list",10000,"",5,"部门管理");
    query.exec(menu.generateInsertSql());
    menu = Menu(100006,"menu","Personal","/personal","","/systemmanage/authmanage/personal/index",10000,"",6,"个人中心");
    query.exec(menu.generateInsertSql());

    DatabaseCenter::getInstance().releaseConnection(db);
}
